function sub=calc_sub(N_i, A_t, ...
    Sunit_t, Scost_t, Skwh_t, P_t, PSale_t, PIns_t, PIns0_t)

global state_max   statetaxcredit_t statemaxtaxcredit_t sunit_cap_t



sub=N_i.*A_t.*(Skwh_t)+Scost_t.*(PIns0_t+ PIns_t.*N_i)+N_i.*Sunit_t; % before state level maxima



if state_max==1
    state_sub=statetaxcredit_t.*(PIns0_t+ PIns_t.*N_i);
    overmax=state_sub>statemaxtaxcredit_t;
    
    sub(overmax)=sub(overmax)-((state_sub(overmax)-statemaxtaxcredit_t(overmax))); %.*statetaxcredit_t(overmax));
          
   unit_sub=N_i.*Sunit_t;
   overmax=unit_sub>sunit_cap_t;
   
    sub(overmax)=sub(overmax)-((unit_sub(overmax)-sunit_cap_t(overmax)));
      
end